<template>
	<view class="auth">
		<view class="card">
      <view class="title">请求授权</view>
      <view class="content">请求获取您的头像信息、昵称</view>
      <view class="btn-group">
        <button type="default">拒绝</button>
        <button type="primary" open-type="getUserInfo" @getuserinfo="onGetUserInfo">同意</button>
      </view>
    </view>
	</view>
</template>

<script>
  import {User} from '@/apis'
  import {utils} from '@/utils'
  
	export default {
		data() {
			return {
				
			}
		},
    methods: {
      onGetUserInfo (e) {
        console.log('获取用户信息：', e)
        let userInfo = e.detail.userInfo
        // 获取openid
        uni.login({
          success (result) {
            console.log('微信登录成功', result)
            User.getUserOpenid({
              data: {
                code: result.code
              }
            }).then(res => {
              if (res.data.code !== 200) {
                utils.wxToast(res.data.message)
                return
              }

              userInfo = utils.assignWithoutNull(userInfo, res.data.body)
              // 同步用户信息
              uni.setStorageSync('userInfo', userInfo)
              // 获取用户全部信息
              User.getUserByOpenid({
                data: {
                  openid: userInfo.openid
                }
              }).then(res => {
                console.log('获取用户详细信息')
                console.log(res)
                // 处理余额
                res.data.body.balance = numeral(res.data.body.balance).divide(100).format('0.00')
                userInfo = utils.assignWithoutNull(userInfo, res.data.body)
                wx.setStorageSync('userInfo', userInfo);
              }).catch(err => console.error(err))
              User.updateUserInfo({
                data: {
                  query: {},
                  pagination: {},
                  form: {
                    nickName: userInfo.nickName,
                    headImg: userInfo.avatarUrl,
                    sex: userInfo.gender,
                    openid: res.data.body.openid
                  }
                }
              }).then(res => {
                console.log('获取信息成功')
                console.log(res)
              }).catch(err => {
                console.error(err)
              }).finally(() => {
                // 不管结果怎么样，跳转到首页
                uni.reLaunch({
                  url: '/pages/index/index'
                })
              })
            }).catch(() => {
              utils.wxToast('授权失败')
            })
          },
          fail (err) {
            console.error('微信登录失败', err)
          },
          complete () {}
        })
      }
    }
	}
</script>

<style lang="scss">
.auth {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba($color: #000000, $alpha: 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  .card {
    position: relative;
    overflow: hidden;
    width: 600rpx;
    height: 530rpx;
    background-color: #FFFFFF;
    border-radius: 10rpx;
    box-sizing: border-box;
    padding-top: 30rpx;
    .title {
      box-sizing: border-box;
      padding: 0 30rpx;
      padding-bottom: 30rpx;
      font-size: 36rpx;
    }
    .content {
      box-sizing: border-box;
      padding-left: 70rpx;
      font-size: 28rpx;
      font-size: #333;
    }
    .btn-group {
      display: flex;
      height: 90rpx;
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      button {
        flex: 1;
        border-radius: unset;
        font-size: 30rpx;
        line-height: 90rpx;
      }
    }
  }
}
</style>
